package com.baidu.fenfayun.demo.model
import io.realm.Realm
import io.realm.RealmResults

open class UserDB(){
   val realm: Realm = Realm.getDefaultInstance()


   fun create(email:String,password:String,username:String="",phone:String = ""){
       realm.beginTransaction()
      var t =  realm.createObject(UserData::class.java,java.util.UUID.randomUUID().toString())
      t.email= email 
      t.username = username
      t.password = password
      realm.commitTransaction()

   }
   fun findOne(username:String):UserData?{
      val alls = realm.where(UserData::class.java).equalTo("username", username).findAll()
      return if(alls.size > 0)  alls.first()else null
   }
   fun findAll():MutableList<UserData>{
      val alls = realm.where(UserData::class.java).findAll()

      val _items = mutableListOf<UserData>()
      for(one in alls){
         _items.add(one) 
      }
      return _items
   }
   fun clearAll(){
      realm.beginTransaction()
      realm.delete(UserData::class.java)
      realm.commitTransaction()

   }
}

